<?php
/**
 * www.zhuizhan.com.
 * User: junyv
 * Date: 14-11-11 下午2:11
 */
//广告
function getAds($board_id=1,$limit=1,$o=1,$site_id=0){
    $board = M('adboard')->where(array('id'=>$board_id,'status'=>1))->find();
    if($board){
        $map = array('board_id'=>$board_id,'status'=>1);
        if($site_id){
            $map['site_id'] = array(array('eq',0),array('eq',$site_id),'or');
        }
        $obj = M('ad')->where($map)->field('type,name,url,img,content,data')->order('ordid desc,id desc');
        if($limit){
            $obj->limit($limit);
        }
        $lists = $obj->select();
        $arr = array();
        if(is_array($lists) && !empty($lists)){
            /*if($limit==1){
                $lists = array_filter($lists,create_function('$v','if($v["site_id"] > 0) return $v;'));
            }*/
            foreach($lists as $k=>$v){
                switch($v['type']){
                    case  'image':
                        $lists[$k]['width'] = $board['width'];
                        $lists[$k]['height'] = $board['height'];
                        ////$lists[$k]['content']    = $v['img'];
                        $lists[$k]['data']   = unserialize($v['data']);
                        $arr[] = "<a href='".$v['url']."' target='_blank' ><img src='".$v['img']."' width='".$board['width']."' /></a>";
                        break;
                    case 'code':
                        $arr[] = $v['content'];

                        break;
                    case 'flash':
                        $arr[] = $v['content'];
                        break;
                    case 'text':
                        $arr[] = "<a href='".$v['url']."' target='_blank'>".$v['content']."</a>";
                        break;
                }

            }
        }
        return $o==1 ? $lists : $arr;
    }else{
        return false;
    }

}
function getAdsOne($board_id,$site_id=0){
    $board = M('adboard')->where(array('id'=>$board_id,'status'=>1))->find();
    if($board){
        $map = array('board_id'=>$board_id,'status'=>1);
       // $map['site_id'] = $site_id;
        $info = M('ad')->where($map)->field('type,name,url,img,content,data')->find();
        if(!$info) return false;
        $info['content'] = htmlspecialchars_decode($info['content']);
        $str = '';
        switch($info['type']){
            case 'image':
                $info['width'] = $board['width'];
                $info['height'] = $board['height'];
               // $info['content'] = $info['img'];
                $str = "<a href='".$info['url']."' target='_blank'><img src='".$info['img']."' width='". $info['width']."' height='".$info['height']."' alt='".$info['name']."' /></a>";
                break;
            case 'code':
               // $info['content'] = $info['code'];
                $str = $info['content'];
                break;
            case 'flash':
                $str = $info['content'];
                break;
            case 'text':
                $str = "<a href='".$info['url']."' target='_blank'>".$info['content']."</a>";
                break;
        }
        return $str;
    }else{
        return false;
    }
}
//位置
function catpos($catid,$model='ArticleCate',$type=1){
    if($type == 1){
        static $arr = array();
    }else{
        static $area = array();
    }
    static $i = 0;
    $catObj = M($model);
    $info = $catObj->where(array('id'=>$catid))->field('id,name,pid,spid,alias')->find();
    if(!isset($info['name'])){
        $info['name'] = '所有分类';
    }else{
        $info['islast'] = $i==0 ? 1 : 0;
        $info['alias'] = trim($info['alias']);
        if($info['pid']){
            $i++;
            catpos($info['pid'],$model,$type);
        }
    }
    if($type==1){
        $arr[] = $info;
        return $arr;
    }else{
        $area[] = $info;
        return $area;
    }

}
function getCateName($id,$model='ArticleCate'){
    $obj = D($model);
    $info = $obj->where(array('id'=>$id))->getField('name');
    return $info;
}
//计算时间差
function getTime($sTime,$type='mohu'){
    if (!$sTime) return '';
    $cTime = time();
    $dTime = $cTime - $sTime;
    $dDay = intval(date("z", $cTime)) - intval(date("z", $sTime));
    $dYear = intval(date("Y", $cTime)) - intval(date("Y", $sTime));
    if($type == 'normal'){
        if($dTime < 60){
            if($dTime < 10){
                return '刚刚';
            }else{
                return intval(floor($dTime / 10) * 10) . "秒前";
            }
        }elseif($dTime < 3600){
            return intval($dTime / 60) . "分钟前";
        }elseif($dYear == 0 && $dDay == 0){
            return '今天' . date('H:i', $sTime);
        }elseif($dYear == 0){
            return date("m月d日 H:i", $sTime);
        }else{
            return date("Y-m-d H:i", $sTime);
        }
    }elseif($type == 'mohu'){
        if($dTime < 60){
            return $dTime . "秒前";
        }elseif($dTime < 3600){
            return intval($dTime / 60) . "分钟前";
        }elseif($dTime >= 3600 && $dDay == 0){
            return intval($dTime / 3600) . "小时前";
        }elseif($dDay > 0 && $dDay <= 7){
            return intval($dDay) . "天前";
        }elseif($dDay > 7 && $dDay <= 30){
            return intval($dDay / 7) . '周前';
        }elseif($dDay > 30){
            return intval($dDay / 30) . '个月前';
        }
    }elseif($type == 'full'){
        return date("Y-m-d , H:i:s", $sTime);
    }elseif($type == 'ymd'){
        return date("Y-m-d", $sTime);
    }else{
        if($dTime < 60){
            return $dTime . "秒前";
        }elseif($dTime < 3600){
            return intval($dTime / 60) . "分钟前";
        }elseif($dTime >= 3600 && $dDay == 0){
            return intval($dTime / 3600) . "小时前";
        }elseif($dYear == 0){
            return date("Y-m-d H:i:s", $sTime);
        }else{
            return date("Y-m-d H:i:s", $sTime);
        }
    }
}

function getUrl($p,$v,$d=''){
    unset($_GET[C('VAR_URL_PARAMS')]);
    $var =  !empty($_POST)?$_POST:$_GET;
    if(is_array($d)){
        foreach($d as $val){
            unset($var[$val]);
        }
    }
    if(empty($var)) {
        $parameter  =   array();
    }else{
        $parameter  =   $var;
    }
    $parameter[$p]  =   '__VAL__';
    $url            =   U('',$parameter);
    return ($p && $v) ? str_replace('__VAL__',$v,$url) : U('',$var);
}
function getNews($site_id,$cate_id,$pagesize=20,$start=0,$pagebar=false,$field="id,title,img,desc,add_time",$order='ordid desc,id desc',$page=1,$condition=''){
    if(is_numeric($cate_id) && $cate_id>0){
        $cateid = get_child_ids($cate_id,true);
    }else{
        $cateid = $cate_id;
    }
    if(!empty($cateid)){
        $where['cate_id'] = array('in',$cateid);
    }
    $obj = M('Article');
    if($site_id){
        $where['site_id'] = $site_id;
    }
    $where['status'] = 1;
    $where['add_time'] = array('lt',time());
    if($condition){
        $where = array_merge($where,$condition);
    }
    if($pagebar){
        $count = $obj->where($where)->count();
        $pageObj = new \Org\Util\Page($count,$pagesize);
        $list    = $obj->where($where)->order($order)->field($field)->page($page,$pageObj->listRows)->select();
        $lists   = array('lists'=>$list,'pages'=>$pageObj->show());
    }else{
        $lists = $obj->where($where)->field($field)->limit($start,$pagesize)->order($order)->select();
    }
    return $lists;
}
function getProduct($site_id,$cate_id,$pagesize=20,$start=0,$pagebar=false,$field="id,title,img,add_time,desc",$order='ordid desc,id desc',$page=1,$condition=''){
    if(is_numeric($cate_id) && $cate_id>0){
        $cateid = get_child_ids($cate_id,true,'ProductCate');
    }else{
        $cateid = $cate_id;
    }
    if(!empty($cateid)){
        $where['cate_id'] = array('in',$cateid);
    }
    $obj = M('Product');
    if($site_id){
        $where['site_id'] = $site_id;
    }
    $where['status'] = 1;
    $where['add_time'] = array('lt',time());
    if($condition){
        $where = array_merge($where,$condition);
    }
    if($pagebar){
        $count = $obj->where($where)->count();
        $pageObj = new \Org\Util\Page($count,$pagesize);
        $list    = $obj->where($where)->field($field)->order($order)->page($page,$pageObj->listRows)->select();
        $lists   = array('lists'=>$list,'pages'=>$pageObj->show());
    }else{
        $lists = $obj->where($where)->field($field)->limit($start,$pagesize)->order($order)->select();
    }
    return $lists;
}

function get_child_ids($id, $with_self=false,$obj='ArticleCate',$field='id',$catechild=true,$menuid=0,$site_id=0) {
    $obj = M($obj);
    $array = array();
    if($id){
        $info = $obj->where(array('id'=>$id))->field('spid,'.$field)->find();
        $spid = $info['spid'];
        if($catechild){
            $spid = $spid ? $spid .= $id .'|' : $id .'|';
        }
        $map['spid'] = array('like', $spid.'%');
        $with_self && $array[] = $info[$field];
    }

    if($site_id) $map['site_id'] = $site_id;
    if($menuid) $map['menuid'] = 1;
    $map['status'] = 1;
    $id_arr = $obj->field($field)->where($map)->select();

    foreach ($id_arr as $val) {
        $array[] = $val[$field];
    }
    $with_self && $array[] = $id;
    return $array;
}
function getPosition($id,$num=3,$site_id=0){
    if($id){
        $info = M('PositionCate')->where(array('id'=>$id))->find();
        if($info){
            $map = array('status'=>1,'cate_id'=>$id);
            if($site_id){
                $map['site_id'] = $site_id;
            }
            $lists = M('Position')->where($map)->limit($num)->select();
           // $obj = D($info['model']);
           // $field = 'id,img,title';
            foreach($lists as $k=>$v){
                $data = unserialize($v['data']);
               // $detail = $obj->where(array('id'=>$data['id']))->field($field)->find();
                //if($detail){
                   // $data = array_merge($data,$detail);
                    $lists[$k]['data'] = $data;
               // }else{
                //    unset($lists[$k]);
               // }
                $lists[$k]['url'] = U(ucfirst($info['model']).'/shows',array('id'=>$v['aid']));
            }
            return $lists;
        }
        return array();
    }
    return array();
}
/**
 * 获取单页文章
 * @param $id
 */
function getPages($id){
        $obj = M('One');
        $info = $obj->where(array('cate_id'=>$id))->find();
    return $info;
}
function jsUnescape($str)
{
    $ret = '';
    $len = strlen($str);
    for ($i = 0; $i < $len; $i++) {
        if ($str[$i] == '%' && $str[$i+1] == 'u') {
            $val = hexdec(substr($str, $i+2, 4));
            if ($val < 0x7f) $ret .= chr($val);
            else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
            else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f));
            $i += 5;
        } else if ($str[$i] == '%') {
            $ret .= urldecode(substr($str, $i, 3));
            $i += 2;
        } else $ret .= $str[$i];
    }
    return $ret;
}
//获取指定时间
function get_date($t='d',$n=0,$a='')
{
    $now_time = time();
    $date=date("Y-m-d",$now_time);
    if($t=='d'){
        $firstday = date('Y-m-d 00:00:00',strtotime("$n day"));
        $lastday = date("Y-m-d 23:59:59",strtotime("$n day"));
    }elseif($t=='w'){
        if($n!=0){$date = date('Y-m-d',strtotime("$n week"));}
        $lastday = date("Y-m-d 00:00:00",strtotime("$date Sunday"));
        $firstday = date("Y-m-d 23:59:59",strtotime("$lastday -6 days"));
    }elseif($t=='m'){
        if($n!=0){$date = date('Y-m-d',strtotime("$n months"));}
        $firstday = date("Y-m-01 00:00:00",strtotime($date));
        $lastday = date("Y-m-d 23:59:59",strtotime("$firstday +1 month -1 day"));
        if($n>0){
            $firstday = $a == 'now' ? date("Y-m-01 00:00:00") : $firstday;
        }else{
            $curdate = date("Y-m-d");
            $firstday = $a == 'now' ? date("Y-m-01 00:00:00",strtotime("$firstday +1 month -1 day")) : $firstday;
            $lastday  =  $a == 'now' ? date("Y-m-d 23:59:59",strtotime("$curdate")) : $lastday;
        }

    }else{
        $firstday = date('Y-m-d 00:00:00');
        $lastday = date("Y-m-d 23:59:59");
    }
    return array($firstday,$lastday);
}
function contentPage($content,$page=1,$pagetag='[page]',$p='page'){
    if(strpos($content,$pagetag)){
        $content_arr = explode($pagetag,$content);
        $totalpage   = count($content_arr);
        unset($_GET[C('VAR_URL_PARAMS')]);
        $var =  !empty($_POST)?$_POST:$_GET;
        if(empty($var)) {
            $parameter  =   array();
        }else{
            $parameter  =   $var;
        }
        $parameter[$p]  =   '__PAGE__';
        $url            =   U('',$parameter);
        $str = '<span>共'.$totalpage.'页</span>';
        if($page==1){
            $pre = '<span>上一页</span></li>';
        }else{
            $upRow = $page-1;
            $pre = '<a href="'.str_replace('__PAGE__',$upRow,$url).'">上一页</a>';
        }
        if($page == $totalpage){
            $next = '<span>下一页</span></li>';
        }else{
            $downRow = $page+1;
            $next = '<a href="'.str_replace('__PAGE__',$downRow,$url).'">下一页</a>';
        }
        $listr = '';
        if($totalpage>1){

            for($i=1;$i<=$totalpage;$i++){
                if($i==$page){
                    $listr .= '<span class="current">'.$i.'</span>';
                }else{
                    $listr .= '<a href="'.str_replace('__PAGE__',$i,$url).'">'.$i.'</a>';
                }
            }
        }
        return array('info'=>$content_arr[($page-1)],'page'=>$str.$pre.$listr.$next);
    }else{
        return array('info'=>$content,'page'=>'');
    }
}